package com.chinasoft.mall.activity.adapter;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.StaffGoodsListActivity;
import com.chinasoft.mall.activity.StaffTopGoodsActivity;
import com.chinasoft.mall.activity.subActivity.goods.GoodsDetailActivity;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.model.response.EmployeeGoodsByClass;
import com.chinasoft.mall.model.response.GoodsByClassBean;
import com.chinasoft.mall.util.LocalDataCommend;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.NetImageView;

public class StaffGoodsListAdapter extends BaseAdapter {
	private List<EmployeeGoodsByClass> dds;
	private LayoutInflater inflater;
	private MainActivity MainActivity;
	private ViewHolder _vh;
	private boolean stateSp = false;
	private Class tag;

	public StaffGoodsListAdapter(MainActivity MainActivity, List<EmployeeGoodsByClass> crs,Class cla) {
		inflater = LayoutInflater.from(MainActivity);
		this.MainActivity = MainActivity;
		this.dds = crs;
		tag = cla;
	}

	public void setData(List<EmployeeGoodsByClass> crs) {
		this.dds = crs;
		this.notifyDataSetChanged();
	}

	public void stateChange() {
		stateSp = !stateSp;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (dds == null) {
			return 0;
		} else {
			return dds.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (dds == null) {
			return null;
		} else {
			return dds.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.sy_recomlv, null);
			_vh = new ViewHolder();
			_vh.modelimg = (NetImageView) convertView
					.findViewById(R.id.modelimg);
			_vh.delete = (ImageView) convertView.findViewById(R.id.delete);
			_vh.spid = (TextView) convertView.findViewById(R.id.spid);
			_vh.sptitle = (TextView) convertView.findViewById(R.id.sptitle);
			_vh.newprice = (TextView) convertView.findViewById(R.id.newprice);
			_vh.oldprice = (TextView) convertView.findViewById(R.id.oldprice);
			convertView.setTag(_vh);
		} else {
			_vh = (ViewHolder) convertView.getTag();
		}
		EmployeeGoodsByClass _dd = dds.get(position);
		_vh.modelimg.setDownloadurl(Config.BASE_URL+"egoods/"+
				_dd.getE_GOODS_ID()+"/"+_dd.getTHUM());
		_vh.spid.setText("19" + _dd.getE_GOODS_ID());
		_vh.sptitle.setText(_dd.getE_GOODS_NAME());
		if(tag.equals(StaffTopGoodsActivity.class)){
			_vh.newprice.setText("￥" + _dd.getACTIVITY_PRICE());
		}else if(tag.equals(StaffGoodsListActivity.class)){
			_vh.newprice.setText("￥" + _dd.getDISCOUNT_PRICE());
		}
		
		_vh.oldprice.setText("￥" + _dd.getMARKET_PRICE());
		if (stateSp) {
			_vh.delete.setVisibility(View.VISIBLE);
		} else {
			_vh.delete.setVisibility(View.GONE);
		}
		convertView.setOnClickListener(new onClickImpl(MainActivity, dds,
				position));
		_vh.delete.setOnClickListener(new onDelClickImpl(MainActivity, dds,
				position));
		return convertView;
	}

	/**
	 * 跳转到员工商城商品详情
	 * 
	 * @author Administrator
	 * 
	 */
	class onClickImpl implements View.OnClickListener {
		private List<EmployeeGoodsByClass> crs;
		private int position;
		private MainActivity MainActivity;

		public onClickImpl(MainActivity MainActivity,
				List<EmployeeGoodsByClass> crs, int position) {
			this.crs = crs;
			this.position = position;
			this.MainActivity = MainActivity;
		}

		@Override
		public void onClick(View v) {
			EmployeeGoodsByClass scd = crs.get(position);
			if (scd != null) {
				MainActivity.setCurrentActivity(GoodsDetailActivity.class, scd);
			}
		}
	}

	/**
	 * 
	 * @author Administrator
	 * 
	 */
	class onDelClickImpl implements View.OnClickListener {
		private List<EmployeeGoodsByClass> crs;
		private int position;
		private MainActivity MainActivity;

		public onDelClickImpl(MainActivity MainActivity,
				List<EmployeeGoodsByClass> crs, int position) {
			this.crs = crs;
			this.position = position;
			this.MainActivity = MainActivity;
		}

		@Override
		public void onClick(View v) {
			EmployeeGoodsByClass gb = crs.get(position);
//			LocalDataCommend.removeGoodsBean(MainActivity, gb);
			crs.remove(position);
			StaffGoodsListAdapter.this.setData(crs);
			MainActivity.refreshLike();
		}
	}

	class ViewHolder {
		public NetImageView modelimg;
		public ImageView delete;
		public TextView spid;
		public TextView sptitle;
		public TextView newprice;
		public TextView oldprice;
	}
}
